package com.abm;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.abm.data.DatabaseManager;
import com.abm.dbclases.Category;
import com.abm.dbclases.Product;
import com.abm.dbclases.ProductLog;
import com.abm.entity.Actividad;
import com.abm.entity.Util;

@SuppressLint("SimpleDateFormat")
public class AddProductActivity extends Actividad {
	private Button btn_cancel;
	private Button btn_save;
	private Dialog dialog_confirmation;
	private Util utilman;
	private DatabaseManager manager;
	private EditText txt_name;
	private EditText txt_quantity;
	private EditText txt_description;
	private Spinner spn_category;
	private Category category;
	private Product product;
	private ProductLog productLog;
	public static Context fotoContext = null;
	public static String PATH_APPDIRs = "sistema_ventas";
	private boolean isEditMode = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_product);

		
		
		Bundle getuserID = getIntent().getExtras();
		if (getuserID != null) {
			if (getIntent().getExtras().getString("MODE") != null) {
				String mode = getIntent().getExtras().getString("MODE");

				if (mode.equals("EDIT")) {
					isEditMode = true;
				}
			}
		}
		
		
		
		
		txt_name = (EditText) findViewById(R.id.addproduct_edittext_name);
		txt_quantity = (EditText) findViewById(R.id.addproduct_edittext_quantity);
		txt_description = (EditText) findViewById(R.id.addproduct_edittext_description);
		spn_category = (Spinner) findViewById(R.id.addproduct_spinner_categorie);

		
		
		utilman = Util.getInstanceUtil();
		manager = DatabaseManager.getInstance();
		
		fillCatSpinner();
		
		mContext = getApplicationContext();

		btn_cancel = (Button) findViewById(R.id.addproduct_button_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btn_save = (Button) findViewById(R.id.addproduct_button_save);
		btn_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				validate();
			}
		});
		
//		spn_category.setOnItemSelectedListener(new OnItemSelectedListener() {
//			@Override
//			public void onItemSelected(AdapterView<?> parentView,
//					View selectedItemView, int position, long id) {
//				// TODO Auto-generated method stub
//				String Text = spn_category.getSelectedItem().toString();
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//
//			}
//
//		});
		
	}
	
	
	
	private void fillCatSpinner() {
		List<Category> list = manager.getAllCategories();

		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(this,
				android.R.layout.simple_spinner_item, list);

		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		// Apply the adapter to the spinner
		spn_category.setAdapter(adapter);
	}
	

	private void validate() {

		if (txt_name.getText().toString().equals("")
				|| txt_name.getText().toString().equals("")
				|| txt_quantity.getText().toString().equals("")
				|| txt_quantity.getText().toString().equals("")) {
			dialog_confirmation = utilman
					.showDialog(
							getString(R.string.complete_all_required_fields_before_continue),
							getString(R.string.atention),
							AddProductActivity.this);
			try {
				dialog_confirmation.show();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {
			
			String name = txt_name.getText().toString();
			int quantity = Integer.parseInt(txt_quantity.getText().toString());
			String description = txt_description.getText().toString();
			category = (Category) spn_category.getSelectedItem();
			
			if (product != null) {
				product.setName(txt_name.getText().toString());
				product.setCategory(category);
				product.setDescription(description);
				product.setQuantity(quantity);
			} else {
				product = new Product(name, quantity, category, description);
				productLog = new ProductLog(name, quantity, description);

				manager.addProduct(product);
				manager.addProductLog(productLog);
			}
			
		}

		finish();
	}
}


